package com.example.mydemo2;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Application;
import android.os.Bundle;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONObject;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        EditText username = findViewById(R.id.tv_name);
        EditText pwd = findViewById(R.id.tv_password);
        EditText phone = findViewById(R.id.tv_phone);
        Button btnRegister = findViewById(R.id.btn_register);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String userName = username.getText().toString();
                String pwd1 = pwd.getText().toString();
                String phone1 = phone.getText().toString();

                User user = new User();
                user.setUsername(userName);
                user.setPwd(pwd1);
                user.setPhone(phone1);

                //System.out.println("用户登录："+user.getUsername());
                if (TextUtils.isEmpty(user.getUsername()) || TextUtils.isEmpty(user.getPwd())) {
                    Toast.makeText(RegisterActivity.this,
                            "用户名或者密码不为空！", Toast.LENGTH_SHORT).show();
                } else {

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                FormBody.Builder params = new FormBody.Builder();
                                params.add("userName", user.getUsername());
                                params.add("userCode", user.getPwd());
                                params.add("phone", user.getPhone());
                                //后补字段
                                params.add("gender","1");
                                MyApplication url = (MyApplication) getApplication();
                                url.getUrl();

                               // System.out.println("aaaaaaaaaa:"+url.getUrl());


                                OkHttpClient client = new OkHttpClient();//创建http客服端
                                Request request = new Request.Builder() //请求ip 数据
                                        .url(url.getUrl()+"register")
                                        .post(params.build())
                                        .build();
                                Response response = client.newCall(request).execute();//执行请求
                                //System.out.println("________________response:"+response);

                                String responseData = response.body().string();
                                System.out.println(responseData);
                                JSONObject jsonObject = new JSONObject(responseData); //创建json对象 传入数据
                                //传入的参数是数据字段名称
                                String status = jsonObject.getString("status");
                                System.out.println("status:" + status);

                                switch (status){
                                    case "200":
//                                        intent.putExtra("user", user);
//                                        startActivity(intent);
                                        Looper.prepare();
                                        Toast.makeText(RegisterActivity.this,
                                                "注册成功", Toast.LENGTH_SHORT).show();
                                        Looper.loop();
                                    case "404":
                                        Looper.prepare();
                                        Toast.makeText(RegisterActivity.this,
                                                "用户名重复！请重新输入", Toast.LENGTH_SHORT).show();
                                        Looper.loop();
                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(RegisterActivity.this, "服务器异常，请联系管理员", Toast.LENGTH_SHORT).show();

                                    }
                                });

                            }

                        }
                    }).start();
                }

            }





        });



    }
}